﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Resources;
using System.IO;
using Microsoft.Win32;
namespace Congducsan
{
    public partial class FrmThancongtron : Form
    {
        public FrmThancongtron()
        {
            InitializeComponent();
        }
        private void FrmThancongtron_FormClosing(Object sender, FormClosingEventArgs e)
        {
            DradApplication.DradApp.FormMain.Show();
        }
        private void tabControl2_Click(object sender, EventArgs e)
        {
            if (tabControl2.SelectedTab == tabControl2.TabPages[1])
            {
                if (Option1.Checked)
                {
                    Option1_CheckedChanged(sender, e);
                }
                else
                {
                    Option2_CheckedChanged(sender, e);
                }
            }
            else
            {
                pictureBox1.Image = Congducsan.Properties.Resources.Troncp;
            }
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Option1_CheckedChanged(object sender, EventArgs e)
        {
            pictureBox1.Image = Congducsan.Properties.Resources.TronBt1;
        }

        private void Option2_CheckedChanged(object sender, EventArgs e)
        {
            pictureBox1.Image = Congducsan.Properties.Resources.TronBt2;
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            this.Hide();
            //PrCongtron frmTron = new PrCongtron();
            PrThan frmTron = new PrThan();
            frmTron.Show();
        }

        private void FrmThancongtron_Load(object sender, EventArgs e)
        {
            loadSettingReg();
            Option2.Checked = true;

        }

        private void FrmThancongtron_Resize(object sender, EventArgs e)
        {
            this.Size = new Size(595, 519);
        }

          //Lưu settings vào registry
        private void saveSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            string Values = "";
            string[] Var1 = regKey.GetValue("KtThan").ToString().Split('\t');
            Values = txtDaymongMin.Text;//0
            Values = Values +"\t"+ txtSDPhandoan.Text;//1
            Values = Values +"\t"+ txtDaydemmong.Text;//2
            Values = Values +"\t"+ txtCachkhoangday.Text;//3
            Values = Values +"\t"+ txtSoday.Text;//4
          //  Values = Values +"\t"+ txtLoaicong.Text;//5
            Values = Values + "\t";
            
            Values = Values +"\t"+ txtDayBt.Text;//6
            Values = Values +"\t"+ txtDayong.Text;//7
          //  Values = Values +"\t"+ txtKhaudien.Text;//8
            Values = Values + "\t";

            Values = Values +"\t"+ Var1[9];//9
            Values = Values +"\t"+ Var1[10];//10
            Values = Values +"\t"+ Var1[11];//11
            Values = Values +"\t"+ txtGocmo.Text;//12
            Values = Values +"\t"+ txtMomong.Text;//13
            Values = Values +"\t"+ Option1.Checked.ToString();//14
            Values = Values +"\t"+ Var1[15];//15
            string Values1;
            string[] Var2 = regKey.GetValue("Vatlieu").ToString().Split('\t');
            Values1 = Var2[0];
            Values1 = Values1 + "\t" + Var2[1];
            Values1 = Values1 + "\t" + Var2[2];
            Values1 = Values1 + "\t" + Var2[3];
            Values1 = Values1 + "\t" + Var2[4];
            Values1 = Values1 + "\t" + Var2[5];
            Values1 = Values1 + "\t" + Var2[6];
            Values1 = Values1 + "\t" + Var2[7];
            Values1 = Values1 + "\t" + Var2[8];
            Values1 = Values1 + "\t" + txtVlMong.Text;
            Values1 = Values1 + "\t" + Var2[10];
           
            string Values2;
            Values2 = txtDapdat.Text;;//0
            Values2 = Values2 +"\t"+ txtTaluydao.Text;// 1
            Values2 = Values2 + "\t" + txtMohomong.Text; //2

            regKey.SetValue("KtThan", Values);
            regKey.SetValue("Vatlieu", Values1);
            regKey.SetValue("Daodap", Values2);
            //regKey.SetValue("GlobalLytrinh", txtLytrinh.Text);
            regKey.Close();
        }
          //Load settings từ registry vào form
        private void loadSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            RegistryKey testKey = regKey.OpenSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            if (testKey != null)
            {
                regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
                string str = regKey.GetValue("KtThan").ToString();
                string[] STR = str.Split('\t');
                int i = 0;
                txtDaymongMin.Text=STR[i++];//0
                txtSDPhandoan.Text=STR[i++];//1
                txtDaydemmong.Text=STR[i++];//2
                txtCachkhoangday.Text=STR[i++];//3
                txtSoday.Text=STR[i++];//4
                //  Values = Values +"\t"+ txtLoaicong.Text;//5
                i++;
                txtDayBt.Text=STR[i++];//6
                txtDayong.Text=STR[i++];//7
                //  Values = Values +"\t"+ txtKhaudien.Text;//8
                i++;
                //Var1[9];//9
                i++;
                //Var1[10];//10
                i++;
                //Var1[11];//11
                i++;
                txtGocmo.Text=STR[i++];//12
                txtMomong.Text=STR[i++];//13
                Option1.Checked=bool.Parse(STR[i++]);//14
                //Var1[15];//15
                i++;
                string[] STR2 = regKey.GetValue("Daodap").ToString().Split('\t');
                
                txtDapdat.Text=STR2[0]; ;//0
                txtTaluydao.Text = STR2[1];// 1
                txtMohomong.Text = STR2[2]; //2
                regKey.Close();
            }
        }
          // Save setting vào file
        private void saveSettingFile()
        {
            SaveFileDialog svDialog = new SaveFileDialog();
            svDialog.Filter = "Data files (*.tron)|*.tron";
            svDialog.InitialDirectory = "C://";
            svDialog.RestoreDirectory = true;
            string filename = "";
            if (svDialog.ShowDialog() == DialogResult.OK)
            {
                filename = svDialog.FileName;
                string Str;
                //Than cong
                Str = "------THAN CONG------" +"\r\n";
                Str = Str + "So day cong:" +"\t"+ txtSoday .Text+"\r\n";
                Str = Str + "Khoang cach day:" +"\t"+ txtCachkhoangday .Text+"\r\n";
                Str = Str + "Chieu cao dat dap:" +"\t"+ txtDapdat .Text+"\r\n";
                Str = Str + "Taluy dao ho mong:" +"\t"+ txtTaluydao .Text+"\r\n";
                Str = Str + "Mo rong ho mong:" +"\t"+ txtMohomong .Text+"\r\n";
                //Mong cong
                Str = Str + "------MONG CONG------" +"\r\n";
                Str = Str + "Kieu mong 1:" +"\t"+ Option1.Checked.ToString()+"\r\n";
                Str = Str + "Day mong H1:" +"\t"+ txtDaymongMin .Text+"\r\n";
                Str = Str + "Day dem lot H2:" +"\t"+ txtDaydemmong .Text+"\r\n";
                Str = Str + "Mo rong mong m1:" +"\t"+ txtMomong .Text+"\r\n";
                Str = Str + "Mo rong mong m2:" +"\t"+ txtLopdemm .Text+"\r\n";
                Str = Str + "Goc mo a:" +"\t"+ txtGocmo .Text+"\r\n";
                Str = Str + "Day mong H:" +"\t"+ txtDaymongT .Text+"\r\n";
                Str = Str + "Rong mong b:" +"\t"+ txtRongmongB .Text+"\r\n";
                Str = Str + "Doc mui luyen i:" +"\t"+ txtDayBt .Text+"\r\n";
                Str = Str + "Ong cong/1 phan doan mong:" +"\t"+ txtSDPhandoan .Text+"\r\n";
                Str = Str + "Vat lieu mong:" + "\t" + txtVlMong.Text;
                //Ghi file dữ liệu
                FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(Str);
                sw.Close();
                fs.Close();
                MessageBox.Show("OK", "Thông báo");
            }
        }
           // load setting từ file vào form
        private void loadSettingFile()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Data files (*.tron)|*.tron";
            openDialog.InitialDirectory = @"C:\";
            openDialog.FilterIndex = 2;
            openDialog.RestoreDirectory = true;
            openDialog.Multiselect = false;
            string filename;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                filename = openDialog.FileName;
                FileStream fs = new FileStream(filename, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string str = sr.ReadToEnd();
                fs.Close();
                fs.Close();
                string[] Values = str.Split('\n');
                int i = 1;
                //Than cong
                txtSoday .Text=Values[i].Split('\t')[1]; i++;
                txtCachkhoangday .Text=Values[i].Split('\t')[1]; i++;
                txtDapdat .Text=Values[i].Split('\t')[1]; i++;
                txtTaluydao .Text=Values[i].Split('\t')[1]; i++;
                txtMohomong .Text=Values[i].Split('\t')[1]; i++;
                //Mong cong
                i = i + 1;
                Option1.Checked=bool.Parse(Values[i].Split('\t')[1]); i++;
                txtDaymongMin .Text=Values[i].Split('\t')[1]; i++;
                int j = i; i = i + 1;
                txtMomong .Text=Values[i].Split('\t')[1]; i++;
                txtLopdemm .Text=Values[i].Split('\t')[1]; i++;
                txtGocmo .Text=Values[i].Split('\t')[1]; i++;
                txtDaymongT .Text=Values[i].Split('\t')[1]; i++;
                txtRongmongB .Text=Values[i].Split('\t')[1]; i++;
                txtDayBt .Text=Values[i].Split('\t')[1]; i++;
                txtSDPhandoan .Text=Values[i].Split('\t')[1]; i++;
                txtVlMong.Text=Values[i].Split('\t')[1]; i++;
                //Thoat khoi su kien click_txtVlMong()         
                txtDaydemmong.Text=Values[j].Split('\t')[1]; i++;
                MessageBox.Show("OK", "Thông báo");
            }
        }

        private void SaveData_Click(object sender, EventArgs e)
        {
            saveSettingFile();
        }

        private void LoadData_Click(object sender, EventArgs e)
        {
            loadSettingFile();
        }

        private void FrmThancongtron_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveSettingReg();
        }
    }
}
